package com.generator.marketManage.orderManage.controller;

import com.generator.marketManage.orderManage.pojo.*;
import com.generator.marketManage.orderManage.service.impl.OrderManageService;
import com.generator.util.DateFormatUtil;
import com.generator.util.PageModel;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/orderManage")
public class OrderManageController {

    @Autowired
    private OrderManageService orderManageService;

    public OrderManageService getOrderManageService() {
        return orderManageService;
    }

    public void setOrderManageService(OrderManageService orderManageService) {
        this.orderManageService = orderManageService;
    }

    @RequestMapping("/queryOrder")
    @ResponseBody
    //条件查询订单
    public PageModel<Order> queryOrder(HttpServletRequest request, OrderCondition condition, PageModel<Order> pageModel){
        String orderid = request.getParameter("orderid");
        String customname = request.getParameter("customname");
        String stime = request.getParameter("startTime");
        String etime = request.getParameter("endTime");
        String minprice = request.getParameter("minprice");
        String maxprice = request.getParameter("maxprice");
        String status = request.getParameter("status");

        if (status!=null){
            if (status.equals("请选择")){
                status = "";
            }
        }
//        Users user = (Users) request.getSession().getAttribute("user");
//        if (user!=null){
//            Integer uId = user.getUId();
//            condition.setLoaduserid(uId);
//        }

        Date startTime = null;
        if (stime!=null&&!stime.equals("")) {
            startTime = DateFormatUtil.stringParseToDate(stime);
        }
        Date endTime = null;
        if (etime!=null&&!etime.equals("")){
            endTime = DateFormatUtil.stringParseToDate(etime);
        }

        condition.setCustomname(customname);
        condition.setOrderid(orderid);
        condition.setMinprice(minprice);
        condition.setMaxprice(maxprice);
        condition.setStartTime(startTime);
        condition.setEndTime(endTime);
        condition.setStatus(status);


        PageModel<Order> m = orderManageService.queryOrder(condition, pageModel);
        if (m!=null&&!m.getData().isEmpty()){
            return m;
        }
        return null;
    }

    @RequestMapping("/putOrderId")
    @ResponseBody
    //存储orderid到session
    public void putOrderId(HttpServletRequest request){
        String orderid = request.getParameter("orderid");
        request.getSession().setAttribute("orderid",orderid);
    }

    //跳转页面获取订单信息
    @RequestMapping("/queryOrderById")
    @ResponseBody
    public List<Order> queryOrderById(HttpServletRequest request, OrderCondition condition){
        String orderid = (String) request.getSession().getAttribute("orderid");
        if (orderid!=""&&orderid!=null){
            condition.setOrderid(orderid);
            return orderManageService.queryOrderById(condition);
        }
        return null;
    }

    //通过订单号查询订单商品详情
    @RequestMapping("/queryOrderDetailByOrderID")
    @ResponseBody
    public List<OrderDetail> queryOrderDetailById(OrderDetail orderDetail, HttpServletRequest request){
        String orderid = (String) request.getSession().getAttribute("orderid");
        if (orderid!=""&&orderid!=null){
            orderDetail.setOrderid(orderid);
            return orderManageService.queryOrderDetailById(orderDetail);
        }
        return null;
    }

    //根据订单号删除订单
    @RequestMapping("/deleteOrder")
    @ResponseBody
    public Integer deleteOrder(OrderCondition condition,HttpServletRequest request){
        String orderid = request.getParameter("orderid");
        condition.setOrderid(orderid);
        return orderManageService.deleteOrder(condition);
    }


    //根据部门查询职位
    @RequestMapping("/queryJobByDept")
    @ResponseBody
    public List<Job> queryJobByDept(Job job,HttpServletRequest request) {
        String deptname = request.getParameter("deptname");
        if (deptname!=null){
            if (!deptname.equals("请选择")){
                job.setDeptname(deptname);
                return orderManageService.queryJobByDept(job);
            }
        }
        return null;
    }


    //根据职位查姓名
    @RequestMapping("/queryNameByJob")
    @ResponseBody
    public List<Job> queryNameByJob(Job job,HttpServletRequest request) {
        String jobname = request.getParameter("jobname");
        if (jobname!=null){
            if (!jobname.equals("请选择")){
                job.setJobname(jobname);
                return orderManageService.queryNameByJob(job);
            }
        }
        return null;
    }

    //提交审核
    @RequestMapping("/subCheck")
    @ResponseBody
    public Integer subCheck(Order order,HttpServletRequest request){
        String orderid = request.getParameter("orderid");
        String uid = request.getParameter("uid");
        if (orderid!=null&&uid!=null){
            order.setCheckid(uid);
            order.setOrderid(orderid);
            return orderManageService.suubCheck(order);
        }
        return null;
    }

    //添加，修改页面加载完成查询所有品牌
    @RequestMapping("/queryBrand")
    @ResponseBody
    public List<Brand> queryBrand(){
        return  orderManageService.queryBrand();
    }

    //根据品牌查询类型
    @RequestMapping("/queryTypeByBrand")
    @ResponseBody
    public List<Type> queryTypeByBrand(Brand brand,HttpServletRequest request){
        String brandId = request.getParameter("brandId");
        if (brandId!=null&&brandId!=""){
            int i = Integer.parseInt(brandId);
            brand.setBrandId(i);
            return  orderManageService.queryTypeByBrand(brand);
        }
        return null;
    }

    //根据类型查询型号
    @RequestMapping("/queryModelByType")
    @ResponseBody
    public List<Product> queryModelByType(Type type,HttpServletRequest request){
        String brandId = request.getParameter("brandId");
        String typeId = request.getParameter("typeId");
        if (typeId!=null&&typeId!=""){
            int i1 = Integer.parseInt(typeId);
            type.setTypeId(i1);
            return  orderManageService.queryModelByType(type);
        }
        return null;
    }

    //根据商品id查商品信息
    @RequestMapping("/queryProductDetail")
    @ResponseBody
    public Product queryProductDetail(Product product,HttpServletRequest request){
        String productId = request.getParameter("productId");
        if (productId!=null&&productId!=""){
            int i = Integer.parseInt(productId);
            product.setProductId(i);
            return orderManageService.queryProductDetail(product);
        }
        return null;
    }

    //保存商品明细
    @ResponseBody
    @RequestMapping("/detailAdd")
    public boolean detailAdd(HttpServletRequest request,@RequestBody List<OrderDetail> orderDetails){
        return orderManageService.detailAdd(orderDetails);
    }

    //pojo改了一下，页面里有些input的有些id属性我改成了value，ajax也改了一些，不过我这边也测试不了
}
